package com.mallify.admin.controller;

import com.mallify.admin.service.AdminLoginService;
import com.mallify.common.request.SystemAdminLoginRequest;
import com.mallify.common.response.MenusResponse;
import com.mallify.common.response.Result;
import com.mallify.common.response.SystemAdminResponse;
import com.mallify.common.response.SystemLoginResponse;
import com.mallify.common.utils.MallifyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 管理端登录服务
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin")
@Api(tags = "管理端登录服务")
public class AdminLoginController {

    @Resource
    private AdminLoginService adminLoginService;

    @ApiOperation(value = "PC登录")
    @PostMapping(value = "/login", produces = "application/json")
    public Result<SystemLoginResponse> systemAdminLogin(@RequestBody @Validated SystemAdminLoginRequest systemAdminLoginRequest, HttpServletRequest request) {
        String ip = MallifyUtil.getClientIp(request);
        SystemLoginResponse systemAdminResponse = adminLoginService.login(systemAdminLoginRequest, ip);
        return Result.success(systemAdminResponse, "login success");
    }

    @PreAuthorize("hasAuthority('admin:logout')")
    @ApiOperation(value = "PC登出")
    @GetMapping(value = "/logout")
    public Result<SystemLoginResponse> systemAdminLogout() {
        adminLoginService.logout();
        return Result.success("logout success");
    }

    @PreAuthorize("hasAuthority('admin:info')")
    @ApiOperation(value = "获取用户详情")
    @GetMapping(value = "/getAdminInfoByToken")
    public Result<SystemAdminResponse> getAdminInfoByToken() {
        return Result.success(adminLoginService.getInfoByToken());
    }

    /**
     * 获取管理员可访问目录
     */
    @PreAuthorize("hasAuthority('admin:login:menus')")
    @ApiOperation(value = "获取管理员可访问目录")
    @GetMapping(value = "/getMenus")
    public Result<List<MenusResponse>> getMenus() {
        return Result.success(adminLoginService.getMenus());
    }
}